package be.djdb.utils.table;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.table.AbstractTableModel;

import be.djdb.Statics;
import be.djdb.UnConstruct;

import be.djdb.utils.Projectresources;
import be.djdb.utils.Projectsettings;
/**
* @author Lieven Roegiers
* @copyright 2011
* @from JAVA_mylibs

*/
@UnConstruct
public class TranslateTableModel extends AbstractTableModel{
    public static final int DESCENDING = -1;
    public static final int NOT_SORTED = 0;
    public static final int ASCENDING = 1;
	private static final long serialVersionUID = 1L;
	private static ResourceBundle translations;
	private Vector<TranslateTableItem> keys = new Vector<TranslateTableItem>();
    private static final String columnNames[] = {"key", "Value","translation" };
    public int getColumnCount() {
      return columnNames.length;
    }
    /**
     * @param translationsPropertieFileName
     */
    public TranslateTableModel(String translationsPropertieFileName) {
    	translations = ResourceBundle.getBundle(translationsPropertieFileName);
	}
    /*
     *  (non-Javadoc)
     * @see javax.swing.table.AbstractTableModel#getColumnName(int)
     */
    public String getColumnName(int column) {
      return columnNames[column];
    }
    /* (non-Javadoc)
     * @see javax.swing.table.TableModel#getRowCount()
     */
    public int getRowCount() {
      return keys.size();
    }
    /* (non-Javadoc)
     * @see javax.swing.table.TableModel#getValueAt(int, int)
     */
    public Object getValueAt(int row, int column) {
      try {
	      if (column == 0) {
	        return keys.elementAt(row).getKey();
	      } else if (column == 1) {
	        return keys.elementAt(row).getValue();
	      } else if (column == 2) {
	    	return keys.elementAt(row).getTranslation();
		  }
      }catch(ArrayIndexOutOfBoundsException ae){
    	  log(Level.ALL, "<ERROR: getValueAt>ArrayIndexOutOfBoundsException Projectsettings properties emty");
      }catch(Exception e){
    	  log(Level.FINEST, "<ERROR: getValueAt> <FINEST>"+e+"</FINEST>");
      }
      return null;
    }
    /**
     * 
     */
    private void init(){
    	keys.removeAllElements();
    }
    /* (non-Javadoc)
     * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
     */
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }
    public boolean isCellEditable(int row, int col) {
        return (col == 3);    
    }
    public void setValueAt(String value, int row, int col) {
        if (Statics.DEBUG){log(Level.FINE, "<SET VALUE> <POS:"+row+","+col+"|TO:"+value+"//"+value.getClass()+">");}
        try{
        	keys.get(row).settranslation(value);
            String key = keys.get(row).getKey();

            fireTableCellUpdated(row, col);
            //fireTableCellUpdated(row, col);
        }catch(Exception e){
        	log(Level.ALL, "<ERROR: SET VALUEAT> <POS:"+row+","+col+"|KEY:"+keys.get(row).toString()+"TO:"+value+"//"+value.getClass()+">");
        	printDebugData();
        	log(Level.FINEST, "<ERROR: setValueAt><FINEST>"+e+"</FINEST>");
        }
        if (Statics.DEBUG) {log(Level.FINE,"<NEW VALUE>");printDebugData();}
    }
	private static  void log(Level level, String msg ){
	    String tag ="<>>>>*"+Statics.LIBNAME+"-"+Statics.COPYRIGHTBY+"*<<<<>StreamManipulator";
	    Logger.getLogger(TranslateTableModel.class.getName()).log(level, tag + msg);
	}
    private void printDebugData() {
        int numRows = getRowCount();
        int numCols = getColumnCount();
        log(Level.FINEST,"-<start>-");
        for (int i=0; i < numRows; i++) {
            log(Level.FINEST,"    row " + i + ":");
            for (int j=0; j < numCols; j++) {
                log(Level.FINEST," + data[i][j]");
            }  
        }
        log(Level.FINEST,"-<end>-");
    }
    
    private synchronized void setDataToTranslate(Hashtable<String, String> table) {
        Set set = table.entrySet();
        init();
        for (String k : table.keySet()) {
        	keys.addElement(new TranslateTableItem(k,table.get(k)));
        }
       fireTableDataChanged();
    }
}
